﻿using Engine;
using Engine.Graphics;
using Game.NetWork;
using GameEntitySystem;
using TemplatesDatabase;

namespace Game
{
    public class ComponentAttackUP : Component, IUpdateable
    {
        public BuffParticleSystem onBuffParticleSystem;

        public bool isadded = false;

        public float ATK_first;

        public float ATKNet;

        public SubsystemGameInfo m_subsystemGameInfo;

        public SubsystemTerrain m_subsystemTerrain;

        public SubsystemTime m_subsystemTime;

        public SubsystemAudio m_subsystemAudio;

        public SubsystemParticles m_subsystemParticles;

        public ComponentPlayer m_componentPlayer;

        public ComponentCreature m_componentCreature;

        public float m_AttackRate;

        public float m_ATKDuration;

        public Random m_random = new Random();

        public bool Isadded1 => isadded;

        public float ATKRate => m_AttackRate;

        public bool IsActive => m_ATKDuration > 0f;

        public UpdateOrder UpdateOrder => UpdateOrder.Default;

        public void StartATKBuff(float healtime, float healrate)
        {
            if (CommonLib.WorkType != WorkType.Client)
            {
                StartATKBuffNet(healtime, healrate);
            }
        }

        public void StartATKBuffNet(float healtime, float healrate)
        {
            m_AttackRate = healrate;
            m_ATKDuration = healtime;
        }

        public void StopBuff()
        {
            if (CommonLib.WorkType != WorkType.Client)
            {
                CommonLib.Net.QueuePackage(new ComponentATKPackage(this));
                if (m_componentPlayer != null)
                {
                    m_ATKDuration = 0f;
                    m_componentPlayer.ComponentGui.DisplaySmallMessage("攻击力强化结束！", Color.DarkRed, blinking: true, playNotificationSound: false);
                    isadded = false;
                    m_componentPlayer.ComponentMiner.AttackPower = ATK_first;
                    ATK_first = 0f;
                }
            }
        }

        public void StopBuffNet()
        {
            if (m_componentPlayer != null)
            {
                m_ATKDuration = 0f;
                m_componentPlayer.ComponentGui.DisplaySmallMessage("攻击力强化结束！", Color.DarkRed, blinking: true, playNotificationSound: false);
                isadded = false;
                m_componentPlayer.ComponentMiner.AttackPower = ATK_first;
                ATK_first = 0f;
            }
        }

        public void Update(float dt)
        {
            if (m_ATKDuration <= 0f && onBuffParticleSystem != null)
            {
                onBuffParticleSystem.IsStopped = true;
                onBuffParticleSystem = null;
            }

            if (!IsActive || !(m_ATKDuration > 0f) || !(ATKRate > 0f))
            {
                return;
            }

            if (!Isadded1)
            {
                if (ATK_first == 0f)
                {
                    ATK_first = m_componentPlayer.ComponentMiner.AttackPower;
                    m_componentPlayer.ComponentMiner.AttackPower = m_componentPlayer.ComponentMiner.AttackPower * (1f + ATKRate / 100f) + 2f;
                }

                isadded = true;
            }

            if (onBuffParticleSystem == null)
            {
                onBuffParticleSystem = new BuffParticleSystem
                {
                    Texture = ContentManager.Get<Texture2D>("Textures/Star")
                };
                m_subsystemParticles.AddParticleSystem(onBuffParticleSystem);
            }

            if (onBuffParticleSystem != null)
            {
                BoundingBox boundingBox = m_componentCreature.ComponentBody.BoundingBox;
                onBuffParticleSystem.Position = 0.5f * (boundingBox.Min + boundingBox.Max);
                onBuffParticleSystem.Radius = 0.5f * MathUtils.Min(boundingBox.Max.X - boundingBox.Min.X, boundingBox.Max.Z - boundingBox.Min.Z);
                onBuffParticleSystem.Color = Color.Lerp(Color.LightRed, Color.DarkRed, MathUtils.Saturate(m_ATKDuration / 20f));
                onBuffParticleSystem.Size = m_componentCreature.ComponentBody.BoxSize.XZ * 0.1f;
            }

            ATKNet = m_componentPlayer.ComponentMiner.AttackPower;
            m_ATKDuration -= dt;
            if (CommonLib.WorkType != WorkType.Client && m_componentPlayer.ComponentHealth.Health > 0f && Time.PeriodicEvent(0.5, 0.0))
            {
                CommonLib.Net.QueuePackage(new ComponentATKPackage(this));
            }

            if (m_ATKDuration < 0f)
            {
                if (onBuffParticleSystem != null)
                {
                    onBuffParticleSystem.IsStopped = true;
                    onBuffParticleSystem = null;
                }

                StopBuff();
            }
        }

        public override void OnEntityRemoved()
        {
            if (onBuffParticleSystem != null)
            {
                onBuffParticleSystem.IsStopped = true;
                onBuffParticleSystem = null;
            }
        }

        public override void Load(ValuesDictionary valuesDictionary, IdToEntityMap idToEntityMap)
        {
            m_subsystemGameInfo = base.Project.FindSubsystem<SubsystemGameInfo>(throwOnError: true);
            m_subsystemTerrain = base.Project.FindSubsystem<SubsystemTerrain>(throwOnError: true);
            m_subsystemTime = base.Project.FindSubsystem<SubsystemTime>(throwOnError: true);
            m_subsystemAudio = base.Project.FindSubsystem<SubsystemAudio>(throwOnError: true);
            m_subsystemParticles = base.Project.FindSubsystem<SubsystemParticles>(throwOnError: true);
            m_componentCreature = base.Entity.FindComponent<ComponentCreature>(throwOnError: true);
            m_componentPlayer = m_componentCreature.Entity.FindComponent<ComponentPlayer>();
            m_ATKDuration = valuesDictionary.GetValue("ATKDuration", 0f);
            m_AttackRate = valuesDictionary.GetValue("ATKRate", 1f);
            ATK_first = valuesDictionary.GetValue("ATKFirst", 0f);
            ATKNet = valuesDictionary.GetValue("ATKNet", 1f);
            if (m_componentPlayer != null && m_ATKDuration > 0f)
            {
                m_componentPlayer.ComponentMiner.AttackPower = ATKNet;
            }
        }

        public override void Save(ValuesDictionary valuesDictionary, EntityToIdMap entityToIdMap)
        {
            valuesDictionary.SetValue("ATKDuration", m_ATKDuration);
            valuesDictionary.SetValue("ATKRate", m_AttackRate);
            valuesDictionary.SetValue("ATKFirst", ATK_first);
            valuesDictionary.SetValue("ATKNet", ATKNet);
        }
    }
}